﻿#include "ledboard.h"
#include <QPainter>
#include <QTimer>
#include <QDebug>
#include <QResizeEvent>

LedBoard::LedBoard(QWidget *parent) : QWidget(parent)
{
    QRect rc = rect();
    m_rcDiv.setRect(36, 30, 128, 64);
    m_rcHomeIcon.setRect(36 + 64, 30+64+16 , 112, 112);
    m_rcClock.setRect(400-96, 30, 192, 64);
    m_rcHomeScore.setRect(400-96, 30+64+32, 64, 64);
    m_rcVistingScore.setRect(400-96+128, 30+64+32, 64, 64);
    m_rcVistingTeam.setRect(400-96+128+64+32, 30+64+32, 64*6, 64);

    m_background_color              = QColor(0x3F,0x4F,0x75);
    m_font_color                    = QColor(0,191,255);

//    this->resize(400,248);

    m_pDiv = new MLabel(this);
    m_pDiv->setGeometry(m_rcDiv.left(), m_rcDiv.top(), m_rcDiv.width(), m_rcDiv.height());
//    m_pDiv->setText("D1");

    m_pHomeIcon = new MLabel(this);
    m_pHomeIcon->setGeometry(m_rcHomeIcon.left(), m_rcHomeIcon.top(), m_rcHomeIcon.width(), m_rcHomeIcon.height());
//    m_pHomeIcon->setImage("d:/NovaProg/HomeICON.png");

    m_pClock = new MLabel(this);
    m_pClock->setGeometry(m_rcClock.left(), m_rcClock.top(), m_rcClock.width(), m_rcClock.height());
//    m_pClock->setText("00:00");

    m_pHomeScore = new MLabel(this);
    m_pHomeScore->setGeometry(m_rcHomeScore.left(), m_rcHomeScore.top(), m_rcHomeScore.width(), m_rcHomeScore.height());
//    m_pHomeScore->setText("0");

    m_pVistingScore = new MLabel(this);
    m_pVistingScore->setGeometry(m_rcVistingScore.left(), m_rcVistingScore.top(), m_rcVistingScore.width(), m_rcVistingScore.height());
//    m_pVistingScore->setText("0");

    m_pVistingTeam = new MLabel(this);
    m_pVistingTeam->setGeometry(m_rcVistingTeam.left(), m_rcVistingTeam.top(), m_rcVistingTeam.width(), m_rcVistingTeam.height());
//    m_pVistingTeam->setText("");
}

void LedBoard::setColor(QColor bkgnd_color, QColor ft_color)
{
    m_background_color              = bkgnd_color;
    m_font_color                    = ft_color;
    this->update();
}

void LedBoard::setLeagueLevel(QString strLeagueLevel)
{
    m_pDiv->setText(strLeagueLevel);
}

void LedBoard::setHomeIcon(QString strLogoFile)
{
    m_pHomeIcon->setImage(strLogoFile);
}

void LedBoard::setHomeScore(int nHomeScore)
{
    QString strHomeScore = QString("%1").arg(nHomeScore);
    m_pHomeScore->setText(strHomeScore);
}

void LedBoard::setVistorScore(int nVisitingTeamScore)
{
    QString strVistorScore = QString("%1").arg(nVisitingTeamScore);
    m_pVistingScore->setText(strVistorScore);
}

void LedBoard::setVisitingTeamName(QString strVisitingTeamName)
{
    m_pVistingTeam->setText(strVisitingTeamName);
}

void LedBoard::setElapseMatchTime(int nElapseSeconds)
{
    QString strClock = QString("%1:%2").arg(nElapseSeconds/60, 2, 10, QLatin1Char('0')).arg(nElapseSeconds%60, 2, 10, QLatin1Char('0'));
    m_pClock->setText(strClock);
}

void LedBoard::setVisitorNameScroll(int nEffect)
{
    if(nEffect == 0)
    {
        m_pVistingTeam->setLoop(false);
        m_pVistingTeam->setPlayEffect(Effect_Static);
    }
    else
    {
        m_pVistingTeam->setLoop(true);
        m_pVistingTeam->setPlayEffect(Effect_ScrollUp);
    }
}

void LedBoard::setRollSpeed(int nSpeed)
{
    m_pVistingTeam->setPlaySpeed(nSpeed);
}

void LedBoard::relayoutComponents()
{
    QRect rcWnd = this->rect();
    qDebug()<<"LedBoard::relayoutComponents(): "<< rcWnd;

    QRectF rcDiv, rcHomeIcon, rcClock, rcHomeScore, rcVisitingScore, rcVistingTeam;

    rcDiv = m_rcDiv;
    rcHomeIcon = m_rcHomeIcon;
    rcClock = m_rcClock;
    rcHomeScore = m_rcHomeScore;
    rcVisitingScore = m_rcVistingScore;
    rcVistingTeam = m_rcVistingTeam;

//    this->resize(400,248);
    int nPosX = (rcWnd.width() - LED_WIDTH) / 2;
    int nPosY = (rcWnd.height() - LED_HEIGHT) / 2;

    qDebug()<<"LedBoard::relayoutComponents(): "<< rcWnd;
//    nPosX = 100;
//    nPosX = 0;

    rcDiv.translate(nPosX, nPosY);
    rcHomeIcon.translate(nPosX, nPosY);
    rcClock.translate(nPosX, nPosY);
    rcHomeScore.translate(nPosX, nPosY);
    rcVisitingScore.translate(nPosX, nPosY);
    rcVistingTeam.translate(nPosX, nPosY);

    m_pDiv->setGeometry(rcDiv.left(), rcDiv.top(), rcDiv.width(), rcDiv.height());
    m_pHomeIcon->setGeometry(rcHomeIcon.left(), rcHomeIcon.top(), rcHomeIcon.width(), rcHomeIcon.height());
    m_pClock->setGeometry(rcClock.left(), rcClock.top(), rcClock.width(), rcClock.height());
    m_pHomeScore->setGeometry(rcHomeScore.left(), rcHomeScore.top(), rcHomeScore.width(), rcHomeScore.height());
    m_pVistingScore->setGeometry(rcVisitingScore.left(), rcVisitingScore.top(), rcVisitingScore.width(), rcVisitingScore.height());
    m_pVistingTeam->setGeometry(rcVistingTeam.left(), rcVistingTeam.top(), rcVistingTeam.width(), rcVistingTeam.height());
}

void LedBoard::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing, true);

    QImage image(800, 280, QImage::Format_RGB32);
    QPainter painterImg(&image);

    image.fill(m_background_color);

//    QBrush brush;
//    brush.setColor(Qt::black);//设置颜色
//    brush.setStyle(Qt::SolidPattern);
//    painterImg.setBrush(brush);

//    painterImg.drawRect(m_rcDiv);//绘制矩形
//    painterImg.drawRect(m_rcHomeIcon);//绘制矩形
//    painterImg.drawRect(m_rcClock);//绘制矩形
//    painterImg.drawRect(m_rcHomeScore);//绘制矩形
//    painterImg.drawRect(m_rcVistingScore);//绘制矩形
//    painterImg.drawRect(m_rcVistingTeam);//绘制矩形

//    QFont font("Arial", 56, 75); //第一个属性是字体（微软雅黑），第二个是大小，第三个是加粗（权重是75）

//    painterImg.setPen(QColor("#FFFFFF"));//字体颜色
//    painterImg.setFont(font);

//    QString strDiv = QString("%1").arg(m_szDiv);
//    painterImg.drawText(m_rcDiv, Qt::AlignCenter | Qt::AlignVCenter, strDiv);  //文字

//    QPixmap imgIcon;
//    imgIcon.load(m_strHomeIconFile);
//    painterImg.drawPixmap(m_rcHomeIcon, imgIcon, imgIcon.rect());

//    QString strClock = QString("%1:%2").arg(m_nElapseSeconds/60, 2, 10, QLatin1Char('0')).arg(m_nElapseSeconds%60, 2, 10, QLatin1Char('0'));
//    painterImg.drawText(m_rcClock, Qt::AlignCenter | Qt::AlignVCenter, strClock);  //文字

//    QString strHomeScore = QString("%1").arg(m_nHomeScore, 1, 10, QLatin1Char('0'));
//    QString strVisitingScore = QString("%1").arg(m_nVisitorScore, 1, 10, QLatin1Char('0'));
//    painterImg.drawText(m_rcHomeScore, Qt::AlignCenter | Qt::AlignVCenter, strHomeScore);  //文字
//    painterImg.drawText(m_rcVistingScore, Qt::AlignCenter | Qt::AlignVCenter, strVisitingScore);  //文字

//    QString strVistorTeam = m_strVisitorName;

//    if(strVistorTeam.length() >= 8)
//    {
//        painterImg.drawText(m_rcVistingTeam, Qt::AlignLeft | Qt::AlignVCenter, strVistorTeam);  //文字
//    }
//    else
//    {
//        painterImg.drawText(m_rcVistingTeam, Qt::AlignCenter | Qt::AlignVCenter, strVistorTeam);  //文字
//    }

    QRectF target(rect()); //建立目标矩形
    QRectF source(0, 0, 400, 320 - 72); //建立源矩形，用来框定源图像文件中要显示的部分
    painter.drawImage(target, image, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
    //(10.0, 20.0, 80.0, 60.0)位置上，大小自适应

    QWidget::paintEvent(event);
}

void LedBoard::resizeEvent(QResizeEvent *ev)
{
    qDebug()<<"old windows: "<<ev->oldSize()<<" current windows: "<<ev->size();
    relayoutComponents();
}
